/*
 * JHeuristics - The Java Meta-Heuritics Library
 * Copyright (C) 2004-2005, Universidade Federal de Campina Grande. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package utils.io;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

public class FileUtils {

	public static URL locate(String filepath, Class caller) throws MalformedURLException {
		if (null == filepath) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}
		if (filepath.trim().equals("")) {
			// TODO: ERROR MSG
			throw new IllegalArgumentException();
		}
		if (null == caller) {
			// TODO: ERROR MSG
			throw new NullPointerException();
		}

		File result = new File(filepath);
		if (result.exists()) {
			return result.toURL();
		}
		result = new File(new File(System.getProperty("user.home")), filepath);
		if (result.exists()) {
			return result.toURL();
		}
		URL url = Thread.currentThread().getContextClassLoader().getResource(filepath);
		if (url != null) {
			return url;
		}
		url = ClassLoader.getSystemResource(filepath);
		if (url != null) {
			return url;
		}
		url = caller.getResource(filepath);
		if (url != null) {
			return url;
		}
		return null;
	}

}
